package com.jv.config;

import com.jv.interceptor.SysInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * web项目配置类
 * @author Jv
 * @version 1.0
 */
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

    //加入跨域映射
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE","OPTIONS")
                .maxAge(3600);
    }

    @Override   //资源处理
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/swiper/**").addResourceLocations("file:G:\\微信小程序项目\\电商图片\\swiperImgs\\");
        registry.addResourceHandler("/image/bigType/**").addResourceLocations("file:G:\\微信小程序项目\\电商图片\\bigTypeImgs\\");
        registry.addResourceHandler("/image/product/**").addResourceLocations("file:G:\\微信小程序项目\\电商图片\\productImgs\\");
        registry.addResourceHandler("/image/productSwiperImgs/**").addResourceLocations("file:G:\\微信小程序项目\\电商图片\\productSwiperImgs\\");
        registry.addResourceHandler("/image/productIntroImgs/**").addResourceLocations("file:G:\\微信小程序项目\\电商图片\\productIntroImgs\\");
        registry.addResourceHandler("/image/productParaImgs/**").addResourceLocations("file:G:\\微信小程序项目\\电商图片\\\\productParaImgs\\");
    }

    //添加鉴权拦截器
    @Bean
    public SysInterceptor sysInterceptor(){
        return new SysInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        java.lang.String[] patterns= new String[]{"/adminLogin","/product/**","/bigType/**","/user/wxlogin","/weixinpay/**"};
        registry.addInterceptor(sysInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(patterns);
    }
}
